import path from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue2'
import Components from 'unplugin-vue-components/vite'
// import { ElementUiResolver } from 'unplugin-vue-components/resolvers'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import AutoImport from 'unplugin-auto-import/vite'

const config = defineConfig({
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src'),
    },
  },

  build: {
    minify: true,
  },

  plugins: [
    vue(),
    Components({
      resolvers: [
        IconsResolver({
          componentPrefix: 'icon',
        }),
        // ElementUiResolver(),
      ],
      // 指定组件位置，默认是src/components
      dirs: ['src/components', 'src/views'],
      extensions: ['vue'],
      dts: './types/components.d.ts',
      version: 2.7,
    }),
    Icons(),
    AutoImport({
      imports: [
        {
          '@vueuse/core': ['useThrottleFn', 'useDebounceFn', 'useCloned'],
          axios: [
            ['default', 'axios'], // import { default as axios } from 'axios',
          ],
          vuex: ['mapState', 'mapActions', 'mapMutations', 'mapGetters'],
          'date-fns': ['format'],
        },
      ],
      // 自动导入函数
      dirs: ['./src/utils', './src/api'],
      dts: './types/auto-imports.d.ts',
      // 在vue模板中使用
      vueTemplate: true,
      eslintrc: {
        enabled: true, // 默认 false
        filepath: './types/.eslintrc-auto-import.json', // 默认 "./.eslintrc-auto-import.json"
        globalsPropValue: true, // 默认 true (true | false | "readonly" | "readable" | "writable" | "writeable")
      },
    }),
  ],

  server: {
    /** 是否开启 HTTPS */
    https: false,
    /** 设置 host: true 才可以使用 Network 的形式，以 IP 访问项目 */
    host: true, // host: "0.0.0.0"
    /** 端口号 */
    port: 3333,
    /** 是否自动打开浏览器 */
    open: false,
    /** 跨域设置允许 */
    cors: true,
    /** 端口被占用时，是否直接退出 */
    strictPort: false,
    /** 接口代理 */
    proxy: {
      '/api': {
        target: 'http://127.0.0.1:3000',
        ws: true,
        /** 是否允许跨域 */
        changeOrigin: true,
        rewrite: path => path.replace('/api', ''),
      },
    },
  },
})

export default config
